<?php
namespace app\index\controller;

use think\Request;
use app\common\model\Order as OrderModel;
use think\Db;

class Order extends Base{

    /**
     * 我的订单列表
     * @param Request $request
     * @throws
     */
    public function index(Request $request){
        $order_list = OrderModel::where(['user_id'=>$this->user->id])->column('id,order_num,total_price,delivery_company,delivery_num,state','id');
        //查找产品图片
        $order_ids = [];
        foreach($order_list as $key => $val ){
            array_push($order_ids,$val['id']);
            $order_list[$key]['picture_list'] = [];
        }
        $order_ids = array_unique($order_ids);

        //SELECT * FROM order_product AS op INNER JOIN product AS p ON  op.`product_id` = p.id WHERE op.order_id IN (1,2);
        $picture_list = Db::table("order_product")->alias("op")->join("product p",'op.product_id = p.id')->where('op.order_id','in',$order_ids)->field('order_id,p.id as product_id,cover')->select();
        foreach( $picture_list as $key => $val ){
            array_push($order_list[$val['order_id']]['picture_list'],$val['cover']);
        }

        $this->success('获取成功','',$order_list);
    }
}